﻿using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for LoginView.xaml
    /// 用于做登录验证
    /// </summary>
    public partial class LoginView : Window
    {
        /// <summary>
        /// 用户的账号
        /// </summary>
        public string UserAccount { get; set; }
        /// <summary>
        /// 用户的密码
        /// </summary>
        public string UserPassword { get; set; }

        /// <summary>
        /// 登陆类型
        /// </summary>
        public string SolutionSelectionMode { get; set; }

        public LoginView()
        {
            InitializeComponent();

            _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;

            UserAccount = Environment.UserName;
            _ctlEditAccount.Text = UserAccount;
        }

        /// <summary>
        /// 创建登录界面，并且直接进行登录认证
        /// </summary>
        /// <param name="userAccount">账号</param>
        /// <param name="userPassword">密码</param>
        /// <param name="solutionSelectionMode">方案选择模式</param>
        public LoginView(string userAccount, string userPassword, string solutionSelectionMode)
        {
            InitializeComponent();

            _ctlEditAccount.Text = userAccount;
            _ctlEditPassword.Password = UserPassword;
            this.SolutionSelectionMode = solutionSelectionMode;
            _ctlBorderLogin.Visibility = System.Windows.Visibility.Hidden;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;
        }

        public LoginView(string userAccount, string userPassword)
        {
            InitializeComponent();

            _ctlEditAccount.Text = userAccount;
            _ctlEditPassword.Password = UserPassword;
            _ctlBorderLogin.Visibility = System.Windows.Visibility.Hidden;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            NCLEventBus.Register(this);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NCLEventBus.Unregister(this);
        }

        /// <summary>
        /// 点击登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_ctlEditAccount.Text))
            {
                return;
            }
            
            _ctlBorderLogin.Visibility = System.Windows.Visibility.Hidden;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Visible;

            UserAccount = _ctlEditAccount.Text;
            UserPassword = _ctlEditPassword.Password;

            BeginLoginEvent @event = new BeginLoginEvent(UserAccount, UserPassword);

            NCLEventBus.Publish(@event);
        }

        private void ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
